import {
  reqEnterpriseLogin,
  reqEnterpriseRegister,
  reqEnterpriseUserInfo,
  reqPageResumes,
  reqDisplayInfo,
  reqRecruits,
  reqRecruit,
  reqResume,
  setDisplayInfo
} from "@/api";
import {setToken,getToken,removeToken} from "@/utils/token";

const state={
  code:"",
  token:getToken(),
  enterpriseInfo:{
    enterpriseUserInfo:{
      email:"",
      gmtCreate:"",
      gmtModified:"",
      username:"",
      eid:"",
      password:"",
      isDeleted:""
    }
  },
  resumeList:{},
  enterpriseDisplay:{},
  recruitsList:{},
  basicResume:{}
}
const mutations={
  ENTERPRISELOGIN(state,token){
    state.token = token
  },
  GETENTERPRISEINFO(state,userInfo){
    state.enterpriseInfo = userInfo
  },
  //清除本地数据
  CLEAR(state){
    state.token='';
    state.enterpriseInfo={}
    //本地存储清空
    removeToken()
  },
  PAGERESUMES(state,data){
    state.resumeList = data.resumeList
  },
  CLEARRESUME(state){
    state.resumeList={}
  },
  DISPLAY(state,data){
    state.enterpriseDisplay = data.enterpriseDisplay
  },
  RECRUITS(state,data){
    state.recruitsList = data.recruitsList
  },
  RESUME(state,data){
    state.basicResume = data.resumeInfo
  }
}
const actions={
  //用户注册
  async enterpriseRegister({commit},user){
    let result = await reqEnterpriseRegister(user);
    if (result.code===20000){
      return 'ok'
    }else {
      return Promise.reject(new Error('false'))
    }
  },
  //登录业务
  async enterpriseLogin({commit},data){
    let result = await reqEnterpriseLogin(data)
    //服务器下发token，将来经常通过token找服务器要用户信息进行展示
    if (result.code===20000){
      //用户已经登陆成功且获取到token
      commit("ENTERPRISELOGIN",result.data.token)
      //持久化存储token
      setToken(result.data.token)
      return 'ok'
    }else {
      return Promise.reject(new Error("false"))
    }
  },
  //获取用户信息
  async getEnterpriseInfo({commit}){
    let result = await reqEnterpriseUserInfo();
    if (result.code===20000){
      //提交用户信息
      commit("GETENTERPRISEINFO",result.data)
      return 'ok'
    }else {
      return Promise.reject(new Error('false'))
    }
  },
  //退出登录
  async enterpriseLogout({commit}){
    commit("CLEAR")
  },
  //获取所有投递的简历
  async getPageResumes({commit},current){
    let result = await reqPageResumes(current);
    if (result.code===20000){
      commit("PAGERESUMES",result.data)
    }else {
      commit("CLEARRESUME")
    }
  },
  //获取企业信息
  async getDisplayInfo({commit},){
    let result = await reqDisplayInfo();
    if (result.code===20000){
      commit("DISPLAY",result.data)
    }
  },
  //获取企业招聘信息
  async getReqRecruits({commit},){
    let result = await reqRecruits();
    if (result.code===20000){
      commit("RECRUITS",result.data)
    }
  },
  //新增企业招聘信息
  async getRecruit({commit},data){
    let result = await reqRecruit(data);
    if (result.code===20000){
      return 'ok'
    }else {
      return Promise.reject(new Error('false'))
    }
  },
  //新增企业信息
  async setDisplayInfo({commit},data){
    console.log(data)
    let result = await setDisplayInfo(data);
    if (result.code===20000){
      return 'ok'
    }else {
      return Promise.reject(new Error('false'))
    }
  },
  //企业用户查看详细简历
  async getResume({commit},id){
    let result = await reqResume(id);
    if (result.code===20000) {
      commit("RESUME", result.data)
    }
  },


}
const getters={}
export default {
  state,
  mutations,
  actions,
  getters
}